define(['base'], function (base) {
    base();

    /**
     * 浏览器检测
     * @returns {string}
     */
    function myBrowser() {
        //取得浏览器的userAgent字符串
        var userAgent = navigator.userAgent;
        //判断是否Opera浏览器
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (isOpera) {
            return "Opera"
        }
        //判断是否Firefox浏览器
        if (userAgent.indexOf("Firefox") > -1) {
            return "FF";
        }

        if (userAgent.indexOf("Chrome") > -1) {
            return "Chrome";
        }

        //判断是否Safari浏览器
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        }
        alert(userAgent);
        //判断是否IE浏览器
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }

        if (!!window.ActiveXObject || "ActiveXObject" in window) {
            return "IE";
        }
    }

    return {
        main: function () {
            $("#ukeySubmit").on("click", function () {
                if ("IE" !== myBrowser()) {
                    $("#errorMessage").html("U-key登录只支持IE浏览器");
                    return;
                }
                try {
                    var lRet = initXaca("XACA", "", "");
                    if (lRet !== 0) {
                        return false;
                    }
                } catch (e) {
                    alert('请确保电脑安装证书小精灵插入UKey！');
                }
                var signCert = GetCert(SIGN_TYPE);
                var info = GetCertInfo(signCert, X509_CERT_SUBJECT_CN);
                var signdata = SignDataByP7(GenRandom(16), ks_hash_alg);
                if (signdata === '' || typeof(signdata) === 'undefined') {
                    alert("签名失败");
                    return false;
                } else {
                    var ret = XacaVerifyP7SignData(signdata);
                    if (ret === 0) {
                        var form = $("#ukeyLogin form");
                        form.find("#ukey").val(info);
                        form.attr("action", "/admin/ukeyLogin").submit();
                    }
                }
            })

        }
    };
});






